//
// Created by Senhai Xu on 2025/3/17.
//BM15 删除有序链表中重复的元素-I
//
struct ListNode
{
  int val;
  struct ListNode *next;
  ListNode(int x) : val(x), next(nullptr) {}
};
#include<unordered_set>
class Solution {

public:
  /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param head ListNode类
     * @return ListNode类
   */
  ListNode* deleteDuplicates(ListNode* head) {
    // write code here
    std::unordered_set<int> set;
    ListNode* cur = head;
    ListNode* pre = new ListNode(0);
    pre->next = head;
    while (cur) {
      if (set.find(cur->val) == set.end()) { // 未找到
        set.insert(cur->val);
        cur = cur->next;
        pre = pre->next;
      } else {
        pre->next = cur->next;
        cur->next = nullptr;
        cur = pre->next;
      }
    }
    return head;
  }
};